import java.lang.invoke.StringConcatFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//通过这个类在内存中构造索引结构
public class Index {

    //使用数组下标表示docId
    private ArrayList<DocInfo> forwardIndex=new ArrayList<>();

    //使用哈希表来表示倒排索引
    //key就是词
    //value就是一组和这个词关联的文章
    private HashMap<String,ArrayList<Weight>> invertedIndex=new HashMap<>();


    //1.给定一个docId，在正排索引中，查询文档详细信息
    public DocInfo getDocInfo(int docId){
        //todo
        return forwardIndex.get(docId);
    }

    //2.给定一个词，在倒排索引查哪些文档和这个词有关
    public List<Weight> getInverted(String term){
        //todo
        return invertedIndex.get(term);
    }

    //3.往索引中新增一个文档
    public void addDoc(String title, String url, String content){
        //新增文档操作，需要同时给正排索引和倒排索引增加信息
        //构建正排索引
        DocInfo docInfo=buildForward(title,url,content);

        //构建倒排索引
        buildInverted(docInfo);
    }

    private void buildInverted(DocInfo docInfo) {
        //1.针对文档标题进行分词
        //2.遍历每个词出现的次数
        //3.针对正文进行分词

    }

    private DocInfo buildForward(String title, String url, String content) {
        DocInfo docInfo=new DocInfo();
        docInfo.setDocId(forwardIndex.size());
        docInfo.setTitle(title);
        docInfo.setUrl(url);
        docInfo.setContent(content);

        forwardIndex.add(docInfo);
        return docInfo;
    }

    //4.把内存的索引结构到保存磁盘
    public void save(){

    }

    //5.把磁盘的索引结构加载到内存
    public void load(){

    }
}
